

package com.springcloud.sc.upms.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springcloud.sc.comm.core.constant.CommonConstants;
import com.springcloud.sc.comm.core.constant.SecurityConstants;
import com.springcloud.sc.upms.api.entity.SysDept;
import com.springcloud.sc.upms.api.entity.SysRole;
import com.springcloud.sc.upms.service.SysRoleMenuService;
import com.springcloud.sc.upms.service.SysRoleService;
import com.springcloud.sc.comm.core.util.R;
import com.springcloud.sc.comm.log.annotation.SysLog;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zhl
 * @date 2017/11/5
 */
@RestController
@AllArgsConstructor
@RequestMapping("/role")
@Api(value = "role", tags = "角色管理模块")
public class RoleController {
	private final SysRoleService sysRoleService;
	private final SysRoleMenuService sysRoleMenuService;

	/**
	 * 通过ID查询角色信息
	 *
	 * @param id ID
	 * @return 角色信息
	 */
	@GetMapping("/{id}")
	public R getById(@PathVariable Integer id) {
		return R.ok(sysRoleService.getById(id));
	}

	/**
	 * 添加角色
	 *
	 * @param sysRole 角色信息
	 * @return success、false
	 */
	@SysLog("添加角色")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('sys_role_add')")
	public R save(@Valid @RequestBody SysRole sysRole) {
		return R.ok(sysRoleService.save(sysRole));
	}

	/**
	 * 修改角色
	 *
	 * @param sysRole 角色信息
	 * @return success/false
	 */
	@SysLog("修改角色")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('sys_role_edit')")
	public R update(@Valid @RequestBody SysRole sysRole) {
		return R.ok(sysRoleService.updateById(sysRole));
	}

	/**
	 * 删除角色
	 *
	 * @param id
	 * @return
	 */
	@SysLog("删除角色")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('sys_role_del')")
	public R removeById(@PathVariable Integer id) {
		return R.ok(sysRoleService.removeRoleById(id));
	}

	/**
	 * 获取角色列表
	 *
	 * @return 角色列表
	 */
	@GetMapping("/list")
	public R listRoles() {
		return R.ok(sysRoleService.list(Wrappers.emptyWrapper()));
	}

	@GetMapping("/list/{tenantId}")
	public R listRolesByTenantId(@PathVariable Integer tenantId) {
		return R.ok(
				sysRoleService.list(
						Wrappers.<SysRole>query().lambda().eq(SysRole::getTenantId , tenantId )
				)
		);
	}

	@GetMapping("/list/{tenantId}/{parentTenantId}")
	public R listRolesByTenant(@PathVariable Integer tenantId , @PathVariable Integer parentTenantId) {
		List list = null;
		if (parentTenantId!=null && parentTenantId == Integer.valueOf(CommonConstants.SUPER_TENANT_CODE) ){
			list = sysRoleService.list( Wrappers.emptyWrapper());
		}else{
			list = sysRoleService.list(
					Wrappers.<SysRole>query().lambda().eq(SysRole::getTenantId , tenantId )
			);
		}
		return R.ok(list);
	}

	/**
	 * 分页查询角色信息
	 *
	 * @param page 分页对象
	 * @return 分页对象
	 */
	@GetMapping("/page")
	public R getRolePage(Page page , @RequestParam Map<String, Object> params) {
		String tel = params.get("tenantId")!=null ? params.get("tenantId").toString():"null";
		if (tel == null || tel.equals(CommonConstants.SUPER_TENANT_CODE)){
			return R.ok(sysRoleService.page(page, Wrappers.emptyWrapper()));
		}else{
			Integer tenantId = Integer.valueOf( tel );
			return R.ok(sysRoleService.page(page, Wrappers.<SysRole>query().lambda().eq(SysRole::getTenantId , tenantId )));
		}
	}

	/**
	 * 更新角色菜单
	 *
	 * @param roleId  角色ID
	 * @param menuIds 菜单ID拼成的字符串，每个id之间根据逗号分隔
	 * @return success、false
	 */
	@SysLog("更新角色菜单")
	@PutMapping("/menu")
	@PreAuthorize("@pms.hasPermission('sys_role_perm')")
	public R saveRoleMenus(Integer roleId, @RequestParam(value = "menuIds", required = false) String menuIds) {
		SysRole sysRole = sysRoleService.getById(roleId);
		return R.ok(sysRoleMenuService.saveRoleMenus(sysRole.getRoleCode(), roleId, menuIds));
	}
}
